package org.jeecg.modules.mobile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.basic.customer.entity.BasicCustomer;
import org.jeecg.modules.basic.customer.service.IBasicCustomerService;
import org.jeecg.modules.basic.material.entity.BasicMaterial;
import org.jeecg.modules.basic.material.service.IBasicMaterialService;
import org.jeecg.modules.basic.print.entity.PrintConfig;
import org.jeecg.modules.basic.print.entity.WmsDocumentTypeStatus;
import org.jeecg.modules.basic.print.service.IPrintConfigService;
import org.jeecg.modules.basic.print.service.IWmsDocumentTypeStatusService;
import org.jeecg.modules.basic.supplier.entity.BasicSupplier;
import org.jeecg.modules.basic.supplier.service.IBasicSupplierService;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouse;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouseArea;
import org.jeecg.modules.basic.warehouse.entity.WmsBasicWarehouseAreaLocation;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseAreaLocationService;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseAreaService;
import org.jeecg.modules.basic.warehouse.service.IWmsBasicWarehouseService;
import org.jeecg.modules.basic.warehouseGroup.entity.WmsBasicWarehouseGroupUser;
import org.jeecg.modules.basic.warehouseGroup.entity.WmsBasicWarehouseGroupWarehouse;
import org.jeecg.modules.basic.warehouseGroup.service.IWmsBasicWarehouseGroupUserService;
import org.jeecg.modules.basic.warehouseGroup.service.IWmsBasicWarehouseGroupWarehouseService;
import org.jeecg.modules.business.doc.business.entity.WmsInReceipt;
import org.jeecg.modules.business.doc.business.entity.WmsInReceiptMaterial;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptMaterialService;
import org.jeecg.modules.business.doc.business.service.IWmsInReceiptService;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceiptMaterial;
import org.jeecg.modules.business.doc.outbound.entity.vo.OutboundReceiptLoadCheckFullVO;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptLoadCheckService;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptMaterialService;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTask;
import org.jeecg.modules.business.outbound.entity.WmsOutReceiptTaskMaterial;
import org.jeecg.modules.business.outbound.entity.vo.OutboundTaskUnPickVO;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskMaterialService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskPickService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.repertory.entity.WmsRepertory;
import org.jeecg.modules.business.repertory.service.IWmsRepertoryService;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceipt;
import org.jeecg.modules.business.transfer.entity.WmsMoveReceiptMaterial;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptMaterialService;
import org.jeecg.modules.business.transfer.service.IWmsMoveReceiptService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTaskMaterial;
import org.jeecg.modules.business.warehouse.entity.vo.ListByMaterialIdOutVO;
import org.jeecg.modules.business.warehouse.entity.vo.ListByMaterialIdResponseVO;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskMaterialService;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.dock.entity.WmsLightConfig;
import org.jeecg.modules.dock.entity.light.request.LightRequestVO;
import org.jeecg.modules.dock.mapper.WmsLightConfigMapper;
import org.jeecg.modules.dock.service.LightIBSDockingService;
import org.jeecg.modules.flow.callback.enums.InboundStatusEnum;
import org.jeecg.modules.mobile.entity.constant.MobileTaskStatusConstant;
import org.jeecg.modules.mobile.entity.vo.*;
import org.jeecg.modules.mobile.mapper.MobileHomePageMapper;
import org.jeecg.modules.mobile.service.IMobileMyTaskService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 移动端-我的任务
 * @Author: jeecg-boot
 * @Date: 2023-08-24
 * @Version: V1.0
 */
@Service
public class MobileMyTaskServiceImpl implements IMobileMyTaskService {

    @Autowired
    private IWmsInReceiptTaskMaterialService wmsInReceiptTaskMaterialService;

    @Autowired
    private IWmsOutReceiptTaskMaterialService wmsOutReceiptTaskMaterialService;

    @Autowired
    private IBasicMaterialService materialService;

    @Autowired
    private IWmsBasicWarehouseGroupUserService wmsBasicWarehouseGroupUserService;

    @Autowired
    private IWmsInReceiptTaskService wmsInReceiptTaskService;

    @Autowired
    private IWmsOutReceiptTaskService wmsOutReceiptTaskService;

    @Autowired
    private IWmsInReceiptService wmsInReceiptService;

    @Autowired
    private IWmsInReceiptMaterialService wmsInReceiptMaterialService;

    @Autowired
    private IWmsOutReceiptService wmsOutReceiptService;

    @Autowired
    private IWmsOutReceiptMaterialService wmsOutReceiptMaterialService;

    @Autowired
    private IWmsMoveReceiptService wmsMoveReceiptService;

    @Autowired
    private IWmsMoveReceiptMaterialService moveReceiptMaterialService;

    @Autowired
    private IWmsBasicWarehouseGroupWarehouseService wmsBasicWarehouseGroupWarehouseService;

    @Autowired
    private IWmsBasicWarehouseService wmsBasicWarehouseService;

    @Autowired
    private IWmsBasicWarehouseAreaService wmsBasicWarehouseAreaService;

    @Autowired
    private IWmsBasicWarehouseAreaLocationService wmsBasicWarehouseAreaLocationService;

    @Autowired
    private IWmsRepertoryService wmsRepertoryService;

    @Resource
    private ISysBaseAPI iSysBaseAPI;

    @Resource
    private MobileHomePageMapper mobileHomePageMapper;

    @Autowired
    private IBasicCustomerService customerService;

    @Autowired
    private IBasicSupplierService supplierService;

    @Autowired
    private IWmsDocumentTypeStatusService documentTypeStatusService;

    @Autowired
    private IPrintConfigService printConfigService;

    @Resource
    private ISysDictService sysDictService;

    @Resource
    private WmsLightConfigMapper lightConfigMapper;

    @Autowired
    private LightIBSDockingService lightIBSDockingService;

    @Autowired
    private IWmsOutReceiptTaskPickService wmsOutReceiptTaskPickService;

    @Autowired
    private IWmsOutReceiptLoadCheckService wmsOutReceiptLoadCheckService;

    /**
     * 根据批次号查询物料信息
     *
     * @param wmsInReceiptTaskId
     * @param batchCode
     * @return
     */
    @Override
    public BasicMaterial getMaterialByBatch(String wmsInReceiptTaskId, String batchCode) {
        if (StringUtils.isBlank(batchCode) || StringUtils.isBlank(wmsInReceiptTaskId)) {
            return null;
        }
        List<WmsInReceiptTaskMaterial> materials = wmsInReceiptTaskMaterialService.selectByMainId(wmsInReceiptTaskId);
        WmsInReceiptTaskMaterial material = materials.stream().filter(a -> batchCode.equals(a.getBatchCode())).findFirst().orElse(null);
        if (material == null) {
            return null;
        }
        BasicMaterial byId = materialService.getById(material.getWmsBasicMaterialId());
        return byId;
    }

    /**
     * 查询用户的任务列表
     *
     * @return
     */
    @Override
    public List<MobileMyTaskVO> listMyTask(String warehouseId, String status) {
        List<MobileMyTaskVO> vos = new ArrayList<>();
        // 入库任务
        List<WmsInReceiptTaskMaterial> inList = wmsInReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                .eq(WmsInReceiptTaskMaterial::getWmsBasicWarehouseId, warehouseId));
        if (CollectionUtils.isNotEmpty(inList)) {
            List<DictModel> inDicts = iSysBaseAPI.getDictItems("document_type");
            Map<String, String> inMaps = inDicts.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
            List<BasicSupplier> suppliers = supplierService.list();
            Map<String, String> supplierMaps = suppliers.stream().collect(Collectors.toMap(BasicSupplier::getId, BasicSupplier::getName, (key1, key2) -> key2));
            Set<String> inTaskIds = inList.stream().map(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId).collect(Collectors.toSet());
            List<WmsInReceiptTask> inTasks = wmsInReceiptTaskService.list(new LambdaQueryWrapper<WmsInReceiptTask>()
                    .in(WmsInReceiptTask::getId, inTaskIds)
                    .eq(WmsInReceiptTask::getWarehouseStatus, status)
                    .eq(!"3".equals(status), WmsInReceiptTask::getDocumentStatus, "2")
                    .orderByDesc(WmsInReceiptTask::getCreateTime));
            for (WmsInReceiptTask inTask : inTasks) {
                MobileMyTaskVO vo = BeanUtil.toBean(inTask, MobileMyTaskVO.class);
                vo.setDocumentType(inMaps.get(inTask.getDocumentType()));
                vo.setType("1");
                vo.setCustomerSupplier(supplierMaps.get(inTask.getWmsBasicSupplierId()));
                vos.add(vo);
            }
        }
        // 出库任务
        List<WmsOutReceiptTaskMaterial> outList = wmsOutReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>()
                .eq(WmsOutReceiptTaskMaterial::getWmsBasicWarehouseId, warehouseId));
        if (CollectionUtils.isNotEmpty(outList)) {
            List<DictModel> outDicts = iSysBaseAPI.getDictItems("outbound_type");
            Map<String, String> outMaps = outDicts.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
            List<BasicCustomer> customers = customerService.list();
            Map<String, String> customerMaps = customers.stream().collect(Collectors.toMap(BasicCustomer::getId, BasicCustomer::getName, (key1, key2) -> key2));
            Set<String> outTaskIds = outList.stream().map(WmsOutReceiptTaskMaterial::getWmsOutReceiptTaskId).collect(Collectors.toSet());
            List<WmsOutReceiptTask> outTasks = wmsOutReceiptTaskService.list(new LambdaQueryWrapper<WmsOutReceiptTask>()
                    .in(WmsOutReceiptTask::getId, outTaskIds)
                    .eq(WmsOutReceiptTask::getOutboundStatus, status)
                    .eq(!"3".equals(status), WmsOutReceiptTask::getDocumentStatus, "2")
                    .orderByDesc(WmsOutReceiptTask::getCreateTime));
            for (WmsOutReceiptTask outTask : outTasks) {
                MobileMyTaskVO vo = BeanUtil.toBean(outTask, MobileMyTaskVO.class);
                vo.setDocumentType(outMaps.get(outTask.getDocumentType()));
                vo.setType("2");
                vo.setCustomerSupplier(customerMaps.get(outTask.getCustomer()));
                vos.add(vo);
            }
        }
        return vos;
    }

    /**
     * 查询用户的单据列表
     *
     * @return
     */
    @Override
    public List<MobileMyDocumentVO> listMyDocument(String warehouseId, String type) {
        List<MobileMyDocumentVO> vos = new ArrayList<>();
        if (StringUtils.isBlank(type) || "1".equals(type)) {
            // 入库单
            List<WmsInReceiptMaterial> inList = wmsInReceiptMaterialService.list(new LambdaQueryWrapper<WmsInReceiptMaterial>()
                    .eq(WmsInReceiptMaterial::getWmsBasicWarehouseId, warehouseId));
            if (CollectionUtils.isNotEmpty(inList)) {
                Set<String> inIds = inList.stream().map(WmsInReceiptMaterial::getWmsInReceiptId).collect(Collectors.toSet());
                List<DictModel> inDicts = iSysBaseAPI.getDictItems("document_type");
                Map<String, String> inMaps = inDicts.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
                List<WmsInReceipt> ins = wmsInReceiptService.list(new LambdaQueryWrapper<WmsInReceipt>()
                        .in(WmsInReceipt::getId, inIds)
                        .eq(WmsInReceipt::getDocumentStatus, "2")
                        .orderByDesc(WmsInReceipt::getCreateTime));
                for (WmsInReceipt in : ins) {
                    MobileMyDocumentVO vo = BeanUtil.toBean(in, MobileMyDocumentVO.class);
                    vo.setDocumentType(inMaps.get(in.getDocumentType()));
                    vo.setType("1");
                    vo.setTaskDate(in.getWarehousingDate());
                    vos.add(vo);
                }
            }
        }
        if (StringUtils.isBlank(type) || "2".equals(type)) {
            // 出库单
            List<WmsOutReceiptMaterial> outList = wmsOutReceiptMaterialService.list(new LambdaQueryWrapper<WmsOutReceiptMaterial>()
                    .eq(WmsOutReceiptMaterial::getWmsBasicWarehouseId, warehouseId));
            if (CollectionUtils.isNotEmpty(outList)) {
                Set<String> outIds = outList.stream().map(WmsOutReceiptMaterial::getWmsOutReceiptId).collect(Collectors.toSet());
                List<DictModel> outDicts = iSysBaseAPI.getDictItems("outbound_type");
                Map<String, String> outMaps = outDicts.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
                List<WmsOutReceipt> outs = wmsOutReceiptService.list(new LambdaQueryWrapper<WmsOutReceipt>()
                        .in(WmsOutReceipt::getId, outIds)
                        .eq(WmsOutReceipt::getDocumentStatus, "2")
                        .orderByDesc(WmsOutReceipt::getCreateTime));
                for (WmsOutReceipt out : outs) {
                    MobileMyDocumentVO vo = BeanUtil.toBean(out, MobileMyDocumentVO.class);
                    vo.setDocumentType(outMaps.get(out.getDocumentType()));
                    vo.setType("2");
                    vo.setTaskDate(out.getOutDate());
                    vos.add(vo);
                }
            }
        }
        if (StringUtils.isBlank(type) || "3".equals(type)) {
            // 移库单
            List<DictModel> moveDicts = iSysBaseAPI.getDictItems("transfer_type");
            Map<String, String> moveMaps = moveDicts.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
            List<WmsMoveReceipt> moves = wmsMoveReceiptService.list(new LambdaQueryWrapper<WmsMoveReceipt>()
                    .in(WmsMoveReceipt::getTransferFromWarehouseId, warehouseId)
                    .eq(WmsMoveReceipt::getDocumentStatus, "2")
                    .orderByDesc(WmsMoveReceipt::getCreateTime));
            for (WmsMoveReceipt move : moves) {
                MobileMyDocumentVO vo = BeanUtil.toBean(move, MobileMyDocumentVO.class);
                vo.setDocumentType(moveMaps.get(move.getTransferType()));
                vo.setType("3");
                vo.setTaskDate(move.getBusinessDate());
                vos.add(vo);
            }
        }
        return vos;
    }

    /**
     * 查询用户所属仓库
     *
     * @param userId
     * @return
     */
    @Override
    public List<WmsBasicWarehouse> getWarehouse(String userId) {
        List<WmsBasicWarehouseGroupUser> list = wmsBasicWarehouseGroupUserService.list(new LambdaQueryWrapper<WmsBasicWarehouseGroupUser>()
                .eq(WmsBasicWarehouseGroupUser::getUserId, userId));
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<String> warehouseGroupIds = list.stream().map(WmsBasicWarehouseGroupUser::getWmsBasicWarehouseGroupId).collect(Collectors.toList());
        List<WmsBasicWarehouseGroupWarehouse> warehouses = wmsBasicWarehouseGroupWarehouseService.list(new LambdaQueryWrapper<WmsBasicWarehouseGroupWarehouse>()
                .in(WmsBasicWarehouseGroupWarehouse::getWmsBasicWarehouseGroupId, warehouseGroupIds));
        if (CollectionUtils.isEmpty(warehouses)) {
            return Collections.emptyList();
        }
        List<String> warehouseIds = warehouses.stream().map(WmsBasicWarehouseGroupWarehouse::getWmsBasicWarehouseId).collect(Collectors.toList());
        return wmsBasicWarehouseService.listByIds(warehouseIds);
    }

    /**
     * 首页统计数据
     *
     * @param userId
     * @param warehouseId
     * @return
     */
    @Override
    public MobileHomePageVO getHomePage(String userId, String warehouseId) {
        MobileHomePageVO vo = new MobileHomePageVO();
        // 我的任务数
        List<MobileMyTaskVO> notStartTasks = this.listMyTask(warehouseId, MobileTaskStatusConstant.NOT_START);
        List<MobileMyTaskVO> executingTasks = this.listMyTask(warehouseId, MobileTaskStatusConstant.EXECUTING);
        vo.setMyTaskNum(notStartTasks.size() + executingTasks.size());
        // 总库位数
        List<WmsBasicWarehouseArea> areas = wmsBasicWarehouseAreaService.list(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId));
        if (CollectionUtils.isEmpty(areas)) {
            vo.setTotalLocation(0);
            vo.setEmptyLocation(0);
        } else {
            List<String> areaIds = areas.stream().map(WmsBasicWarehouseArea::getId).collect(Collectors.toList());
            List<WmsBasicWarehouseAreaLocation> locations = wmsBasicWarehouseAreaLocationService.list(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                    .in(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, areaIds));
            vo.setTotalLocation(locations.size());
            // 空余库位
            if (CollectionUtils.isEmpty(locations)) {
                vo.setEmptyLocation(0);
            } else {
                List<String> locationIds = locations.stream().map(WmsBasicWarehouseAreaLocation::getId).collect(Collectors.toList());
                List<WmsRepertory> repertoryList = wmsRepertoryService.list(new LambdaQueryWrapper<WmsRepertory>()
                        .in(WmsRepertory::getWmsBasicWarehouseAreaLocationId, locationIds));
                Set<String> set = repertoryList.stream().map(WmsRepertory::getWmsBasicWarehouseAreaLocationId).collect(Collectors.toSet());
                vo.setEmptyLocation(locations.size() - set.size());
            }
        }
        // 百分比
        if (vo.getTotalLocation() != 0) {
            double d1 = vo.getEmptyLocation() * 1.0;
            double d2 = vo.getTotalLocation() * 1.0;
            NumberFormat percentInstance = NumberFormat.getPercentInstance();
            percentInstance.setMinimumFractionDigits(0);    //保留小数位数
            vo.setPercent(percentInstance.format(d1 / d2));
        } else {
            vo.setPercent("0%");
        }
        // 今日入库单
        List<String> inList = mobileHomePageMapper.listInReceipt(warehouseId, LocalDate.now() + " 00:00:00");
        vo.setInReceipt(inList.size());
        // 今日出库单
        List<String> outList = mobileHomePageMapper.listOutReceipt(warehouseId, LocalDate.now() + " 00:00:00");
        vo.setOutReceipt(outList.size());
        // 我的单据数
        List<MobileMyDocumentVO> documents = this.listMyDocument(warehouseId, null);
        vo.setMyDocumentNum(documents.size());
        return vo;
    }

    /**
     * 库存查询
     *
     * @param vo
     * @return
     */
    @Override
    public IPage<WmsRepertory> queryRepertory(Page<WmsRepertory> pageList, MobileRepertoryParamVO vo) {
        if (CollectionUtils.isEmpty(vo.getWarehouseIds())) {
            return new Page<>();
        }
        if (StringUtils.isNotBlank(vo.getCode())) {
            List<WmsBasicWarehouseArea> wmsBasicWarehouseAreas = wmsBasicWarehouseAreaService.list(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                    .in(WmsBasicWarehouseArea::getWmsBasicWarehouseId, vo.getWarehouseIds())
                    .eq(WmsBasicWarehouseArea::getCode, vo.getCode()));
            vo.setWarehouseAreaIds(wmsBasicWarehouseAreas.stream().map(t -> t.getId()).collect(Collectors.toList()));

            List<WmsBasicWarehouseArea> warehouseAreaIdsByHouse = wmsBasicWarehouseAreaService.list(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                    .in(WmsBasicWarehouseArea::getWmsBasicWarehouseId, vo.getWarehouseIds()));
            if (CollectionUtils.isNotEmpty(warehouseAreaIdsByHouse)) {
                List<String> warehouseAreaByHouseIds = warehouseAreaIdsByHouse.stream().map(t -> t.getId()).collect(Collectors.toList());
                List<WmsBasicWarehouseAreaLocation> warehouseAreaLocations = wmsBasicWarehouseAreaLocationService.list(
                        new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                                .in(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, warehouseAreaByHouseIds)
                                .eq(WmsBasicWarehouseAreaLocation::getCode, vo.getCode()));
                vo.setWarehouseAreaLocationIds(warehouseAreaLocations.stream().map(t -> t.getId()).collect(Collectors.toList()));
            }
        }
        if (CollectionUtils.isNotEmpty(vo.getWarehouseAreaIds()) || CollectionUtils.isNotEmpty(vo.getWarehouseAreaLocationIds())) {
            vo.setCode(null);
        }
        return mobileHomePageMapper.pageRepertory(pageList, vo);
//        List<String> warehouseAreaIds = new ArrayList<>();
//        List<String> warehouseAreaLocationIds = new ArrayList<>();
//
//        List<String> finalWarehouseAreaLocationIds = warehouseAreaLocationIds;
//        List<String> finalWarehouseAreaIds = warehouseAreaIds;
//        LambdaQueryWrapper<WmsRepertory> lambdaQueryWrapper = new LambdaQueryWrapper<WmsRepertory>()
//                .in(WmsRepertory::getWmsBasicWarehouseId, vo.getWarehouseIds());
//        if (StringUtils.isNotBlank(vo.getCode())) {
//            lambdaQueryWrapper.and(t -> {
//                t.like(WmsRepertory::getBatchCode, vo.getCode())
//                        .or().like(WmsRepertory::getMaterialCode, vo.getCode())
//                        .or().like(WmsRepertory::getSteelCode, vo.getCode())
//                        .or(CollectionUtils.isNotEmpty(finalWarehouseAreaIds));
//                if (CollectionUtils.isNotEmpty(finalWarehouseAreaIds)) {
//                    t.or().in(WmsRepertory::getWmsBasicWarehouseAreaId, finalWarehouseAreaIds);
//                }
//                if (CollectionUtils.isNotEmpty(finalWarehouseAreaLocationIds)) {
//                    t.or().in(WmsRepertory::getWmsBasicWarehouseAreaLocationId, finalWarehouseAreaLocationIds);
//                }
//            });
//        }
//
//        Page<WmsRepertory> page = new Page<>(ObjectUtil.isNotNull(vo.getPageNo())?vo.getPageNo():1,
//                ObjectUtil.isNotNull(vo.getPageSize())?vo.getPageSize():5);
//        IPage<WmsRepertory> list = wmsRepertoryService.page(page,lambdaQueryWrapper);
//        List<MobileRepertoryVO> vos = org.jeecg.common.util.BeanUtil.copyToList(list.getRecords(), MobileRepertoryVO.class);
//        return CollectionUtil.isNotEmpty(vos)?vos:Collections.emptyList();
//        return list;
    }

    /**
     * 入库任务出库任务小红点
     *
     * @param warehouseId
     * @return
     */
    @Override
    public HashMap<String, Object> getRedNum(String warehouseId) {
        HashMap<String, Object> map = new HashMap<>();
        // 入库任务
        List<WmsInReceiptTaskMaterial> inList = wmsInReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                .eq(WmsInReceiptTaskMaterial::getWmsBasicWarehouseId, warehouseId));
        map.put("inReceiptTask", 0);
        if (CollectionUtils.isNotEmpty(inList)) {
            Set<String> inTaskIds = inList.stream().map(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId).collect(Collectors.toSet());
            long inTaskNum = wmsInReceiptTaskService.count(new LambdaQueryWrapper<WmsInReceiptTask>()
                    .in(WmsInReceiptTask::getId, inTaskIds)
                    .in(WmsInReceiptTask::getDocumentStatus, "2", "5")
            );
            map.put("inReceiptTask", inTaskNum);
        }
        // 出库任务
        List<WmsOutReceiptTaskMaterial> outList = wmsOutReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>()
                .eq(WmsOutReceiptTaskMaterial::getWmsBasicWarehouseId, warehouseId));
        map.put("outReceiptTask", 0);
        if (CollectionUtils.isNotEmpty(outList)) {
            Set<String> outTaskIds = outList.stream().map(WmsOutReceiptTaskMaterial::getWmsOutReceiptTaskId).collect(Collectors.toSet());
            long outTaskNum = wmsOutReceiptTaskService.count(new LambdaQueryWrapper<WmsOutReceiptTask>()
                    .in(WmsOutReceiptTask::getId, outTaskIds)
                    .in(WmsOutReceiptTask::getDocumentStatus, "2", "5")
            );
            map.put("outReceiptTask", outTaskNum);
        }
        return map;
    }

    /**
     * 任务查询
     *
     * @param vo
     * @return
     */
    @Override
    public Page<MobileMyTaskVO> queryTask(Page<MobileMyTaskVO> page, MobileTaskParamVO vo) {
        if ("1".equals(vo.getType())) {
            page.setRecords(mobileHomePageMapper.listInReceiptTask(page, vo));
            if (CollectionUtils.isEmpty(page.getRecords())) {
                return page;
            }
            List<DictModel> taskDicts = iSysBaseAPI.getDictItems("document_task");
            Map<String, String> taskMap = taskDicts.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
            List<DictModel> statusDicts = iSysBaseAPI.getDictItems("document_status");
            Map<String, String> statusMap = statusDicts.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
            Set<String> taskIds = page.getRecords().stream().map(MobileMyTaskVO::getId).collect(Collectors.toSet());
            List<WmsInReceiptTaskMaterial> taskMaterials = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(taskIds)){
                taskMaterials = wmsInReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsInReceiptTaskMaterial>()
                        .in(WmsInReceiptTaskMaterial::getWmsInReceiptTaskId, taskIds));
            }

            List<WmsBasicWarehouse> allWarehouse = wmsBasicWarehouseService.list();
            List<WmsInReceiptTaskMaterial> materials = taskMaterials;
            page.getRecords().forEach(o -> {
                o.setDocumentTypeValue(taskMap.get(o.getDocumentType()));
                o.setDocumentStatusValue(statusMap.get(o.getDocumentStatus()));
                // 仓库编码
                Set<String> ids = materials.stream().filter(a -> ObjectUtil.equal(o.getId(), a.getWmsInReceiptTaskId()))
                        .map(WmsInReceiptTaskMaterial::getWmsBasicWarehouseId)
                        .collect(Collectors.toSet());
                List<WmsBasicWarehouse> warehouses = allWarehouse.stream().filter(a -> ids.contains(a.getId())).collect(Collectors.toList());
                Set<String> set = warehouses.stream().map(WmsBasicWarehouse::getCode).collect(Collectors.toSet());
                o.setWarehouseCodeStr(StringUtils.join(set, ","));
                // 来源系统单号
                Set<String> originalSystemNumbers = materials.stream().filter(a -> ObjectUtil.equal(o.getId(), a.getWmsInReceiptTaskId()))
                        .map(WmsInReceiptTaskMaterial::getOriginalSystemNumber)
                        .collect(Collectors.toSet());
                o.setOriginalSystemNumber(StringUtils.join(originalSystemNumbers, ","));
                //设置客户编码 实体中已经有数据
                //设置供应商编码 实体中已经有数据
                //设置产线名称 实体中已经有数据
            });
        } else if ("2".equals(vo.getType())) {
            page.setRecords(mobileHomePageMapper.listOutReceiptTask(page, vo));
            if (CollectionUtils.isEmpty(page.getRecords())) {
                return page;
            }
            List<DictModel> taskDicts = iSysBaseAPI.getDictItems("outbound_task");
            Map<String, String> taskMap = taskDicts.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
            List<DictModel> statusDicts = iSysBaseAPI.getDictItems("document_status");
            Map<String, String> statusMap = statusDicts.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
            Set<String> taskIds = page.getRecords().stream().map(MobileMyTaskVO::getId).collect(Collectors.toSet());
            List<WmsOutReceiptTaskMaterial> taskMaterials = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(taskIds)){
                taskMaterials = wmsOutReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>()
                        .in(WmsOutReceiptTaskMaterial::getWmsOutReceiptTaskId, taskIds));
            }
            List<WmsBasicWarehouse> allWarehouse = wmsBasicWarehouseService.list();
            List<WmsOutReceiptTaskMaterial> materials = taskMaterials;
            page.getRecords().forEach(o -> {
                o.setDocumentTypeValue(taskMap.get(o.getDocumentType()));
                o.setDocumentStatusValue(statusMap.get(o.getDocumentStatus()));
                // 仓库编码
                Set<String> ids = materials.stream().filter(a -> ObjectUtil.equal(o.getId(), a.getWmsOutReceiptTaskId()))
                        .map(WmsOutReceiptTaskMaterial::getWmsBasicWarehouseId)
                        .collect(Collectors.toSet());
                List<WmsBasicWarehouse> warehouses = allWarehouse.stream().filter(a -> ids.contains(a.getId())).collect(Collectors.toList());
                Set<String> set = warehouses.stream().map(WmsBasicWarehouse::getCode).collect(Collectors.toSet());
                //设置仓库编码集合
                o.setWarehouseCodeStr(StringUtils.join(set, ","));
                // 车牌号
                Set<String> carSet = materials.stream().filter(a -> ObjectUtil.equal(o.getId(), a.getWmsOutReceiptTaskId()) && StringUtils.isNotBlank(a.getCarNumber()))
                        .map(WmsOutReceiptTaskMaterial::getCarNumber)
                        .collect(Collectors.toSet());
                o.setCarNumberStr(StringUtils.join(carSet, ","));
                // 来源系统单号
                Set<String> originalSystemNumbers = materials.stream().filter(a -> ObjectUtil.equal(o.getId(), a.getWmsOutReceiptTaskId()))
                        .map(WmsOutReceiptTaskMaterial::getOriginalSystemNumber)
                        .collect(Collectors.toSet());
                o.setOriginalSystemNumber(StringUtils.join(originalSystemNumbers, ","));
                //设置客户编码 实体中已经有数据
                //设置供应商编码 实体中已经有数据
                //设置产线名称 实体中已经有数据
                //设置出库订单号
                o.setOutboundOrderCode(o.getOriginalSystemNumber());
            });
        } else if ("3".equals(vo.getType())) {
            page.setRecords(mobileHomePageMapper.listMoveReceipt(page, vo));
            List<DictModel> taskDicts = iSysBaseAPI.getDictItems("move_type");
            Map<String, String> taskMap = taskDicts.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
            List<DictModel> statusDicts = iSysBaseAPI.getDictItems("document_status");
            Map<String, String> statusMap = statusDicts.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
            page.getRecords().stream().forEach(o -> {
                o.setDocumentTypeValue(taskMap.get(o.getDocumentType()));
                o.setDocumentStatusValue(statusMap.get(o.getDocumentStatus()));
            });
        } else if ("4".equals(vo.getType())) {
            page.setRecords(mobileHomePageMapper.listTakeStockExecute(page, vo));
            List<DictModel> types = iSysBaseAPI.getDictItems("take_stock_type");
            Map<String, String> typeMap = types.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
            List<DictModel> ranges = iSysBaseAPI.getDictItems("take_stock_range");
            Map<String, String> rangeMap = ranges.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
            List<DictModel> statuses = iSysBaseAPI.getDictItems("take_stock_plan_status");
            Map<String, String> statusMap = statuses.stream().collect(Collectors.toMap(DictModel::getValue, DictModel::getText, (key1, key2) -> key2));
            for (MobileMyTaskVO taskVO : page.getRecords()) {
                taskVO.setTakeStockType_dictText(typeMap.get(taskVO.getTakeStockType()));
                taskVO.setRangeType_dictText(rangeMap.get(taskVO.getRangeType()));
                taskVO.setStatus(statusMap.get(taskVO.getStatus()));
            }
        }
        return page;
    }

    /**
     * 扫码查询库区库位
     *
     * @param name
     * @param warehouseId
     * @return
     */
    @Override
    public MobileWarehouseVO queryWarehouse(String name, String warehouseId) {
        if (StringUtils.isBlank(name) || (!name.startsWith("KW") && !name.startsWith("KQ"))) {
            throw new JeecgBootException("请扫描正确的库区或库位二维码！");
        }
        WmsBasicWarehouse warehouse = wmsBasicWarehouseService.getById(warehouseId);
        if ("N".equals(warehouse.getIsUseArea())) {
            throw new JeecgBootException("该仓库未启用库区管理！");
        }
        MobileWarehouseVO vo = new MobileWarehouseVO();
        if (name.startsWith("KQ")) {
            WmsBasicWarehouseArea one = wmsBasicWarehouseAreaService.getOne(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                    .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId)
                    .eq(WmsBasicWarehouseArea::getCode, name)
                    .last("limit 1"));
            if (one == null) {
                throw new JeecgBootException("库区不存在！");
            }
            if ("0".equals(one.getStatus())) {
                throw new JeecgBootException("该库区已被禁用!");
            }
            List<WmsBasicWarehouseAreaLocation> list = wmsBasicWarehouseAreaLocationService.list(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                    .eq(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, one.getId())
                    .eq(WmsBasicWarehouseAreaLocation::getStatus, "1"));
            if (CollectionUtils.isNotEmpty(list)) {
                throw new JeecgBootException("请扫描该库区下库位的二维码！");
            }
            vo.setWarehouseAreaId(one.getId());
            vo.setWarehouseAreaName(one.getName());
        }
        if (name.startsWith("KW")) {
            WmsBasicWarehouseAreaLocation one = wmsBasicWarehouseAreaLocationService.getOne(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                    .eq(WmsBasicWarehouseAreaLocation::getCode, name)
                    .last("limit 1"));
            if (one == null) {
                throw new JeecgBootException("库位不存在！");
            }
            if ("0".equals(one.getStatus())) {
                throw new JeecgBootException("该库位已被禁用!");
            }
            vo.setWarehouseAreaLocationId(one.getId());
            vo.setWarehouseAreaLocationName(one.getName());
            WmsBasicWarehouseArea area = wmsBasicWarehouseAreaService.getById(one.getWmsBasicWarehouseAreaId());
            if (!warehouseId.equals(area.getWmsBasicWarehouseId())) {
                throw new JeecgBootException("该仓库下没有该库位！");
            }
            vo.setWarehouseAreaId(area.getId());
            vo.setWarehouseAreaName(area.getName());
        }
        return vo;
    }

    /**
     * 查询所有状态
     *
     * @param type
     * @return
     */
    @Override
    public List<WmsDocumentTypeStatus> queryAllStatus(String type) {
        List<PrintConfig> configs = printConfigService.list(new LambdaQueryWrapper<PrintConfig>()
                .eq(PrintConfig::getBusinessType, type));
        List<WmsDocumentTypeStatus> list = new ArrayList<>();
        HashSet<String> set = new HashSet<>();
        for (PrintConfig config : configs) {
            List<WmsDocumentTypeStatus> statuses = documentTypeStatusService.selectByMainId(config.getId());
            for (WmsDocumentTypeStatus status : statuses) {
                if (set.contains(status.getName())) {
                    continue;
                }
                set.add(status.getName());
                list.add(status);
            }
        }
        return list;
    }

    /**
     * 查询入库任务关联入库单列表
     *
     * @param id
     * @return
     */
    @Override
    public List<WmsInReceipt> queryInReceipt(String id, String name) {
        List<WmsInReceipt> list = wmsInReceiptService.list(new LambdaQueryWrapper<WmsInReceipt>()
                .eq(WmsInReceipt::getWmsInReceiptTaskId, id)
                .like(StringUtils.isNotBlank(name), WmsInReceipt::getDocumentCode, name)
                .orderByDesc(WmsInReceipt::getCreateTime));
        return list;
    }

    /**
     * 查询出库任务关联出库单列表
     *
     * @param id
     * @return
     */
    @Override
    public List<WmsOutReceipt> queryOutReceipt(String id, String name) {
        List<WmsOutReceipt> list = wmsOutReceiptService.list(new LambdaQueryWrapper<WmsOutReceipt>()
                .eq(WmsOutReceipt::getWmsOutReceiptTaskId, id)
                .like(StringUtils.isNotBlank(name), WmsOutReceipt::getDocumentCode, name)
                .orderByDesc(WmsOutReceipt::getCreateTime));
        return list;
    }

    /**
     * 安灯
     */
    @Override
    public void lightOn(List<InReceiptStrategyWarehouseVO> list) {
        for (InReceiptStrategyWarehouseVO warehouseVO : list) {
            String warehouseId = warehouseVO.getWarehouseId();
            String warehouseAreaId = warehouseVO.getWarehouseAreaId();
            String warehouseAreaLocationId = warehouseVO.getWarehouseAreaLocationId();
            if (StringUtils.isBlank(warehouseId) && StringUtils.isBlank(warehouseAreaId) && StringUtils.isBlank(warehouseAreaLocationId)) {
                continue;
            }
            List<WmsLightConfig> lights = lightConfigMapper.selectList(new LambdaQueryWrapper<WmsLightConfig>()
                    .eq(StringUtils.isNotBlank(warehouseAreaLocationId), WmsLightConfig::getWmsBasicWarehouseAreaLocationId, warehouseAreaLocationId)
                    .eq(StringUtils.isNotBlank(warehouseAreaId), WmsLightConfig::getWmsBasicWarehouseAreaId, warehouseAreaId)
                    .eq(StringUtils.isNotBlank(warehouseId), WmsLightConfig::getWmsBasicWarehouseId, warehouseId));
            if (CollectionUtils.isEmpty(lights) || lights.size() > 1) {
                continue;
            }
            LightRequestVO vo = new LightRequestVO();
            if (!ObjectUtil.equal(vo.getState(), 0)) {
                warehouseVO.setState(1);
            }
            vo.setState(warehouseVO.getState());
            vo.setLightColor(64);
            vo.setLocationId(lights.get(0).getLocationId()+"-"+lights.get(0).getSiteCode());
            List<LightRequestVO> vos = new ArrayList<>();
            vos.add(vo);
            lightIBSDockingService.lightIBS(vos);
        }
    }

    /**
     * 查询仓库是否有默认库区
     * @param warehouseId
     * @return
     */
    @Override
    public WmsBasicWarehouseArea getDefaultArea(String warehouseId) {
        WmsBasicWarehouseArea one = wmsBasicWarehouseAreaService.getOne(new LambdaQueryWrapper<WmsBasicWarehouseArea>()
                .eq(WmsBasicWarehouseArea::getWmsBasicWarehouseId, warehouseId)
                .eq(WmsBasicWarehouseArea::getByDefault, "1")
                .last("limit 1"));
        if (one != null) {
            long count = wmsBasicWarehouseAreaLocationService.count(new LambdaQueryWrapper<WmsBasicWarehouseAreaLocation>()
                    .eq(WmsBasicWarehouseAreaLocation::getWmsBasicWarehouseAreaId, one.getId()));
            if (count == 0) {
                return one;
            }
        }
        return null;
    }

    /**
     * 入库扫托盘码
     * @param taskId
     * @param code
     * @return
     */
    @Override
    public List<ListByMaterialIdResponseVO> inTrayCode(String taskId, String code) {
        if (StringUtils.isBlank(code)) {
            throw new JeecgBootException("托盘码不能为空");
        }
        List<WmsInReceiptTask> tasks = wmsInReceiptTaskService.list(new LambdaQueryWrapper<WmsInReceiptTask>()
                .eq(WmsInReceiptTask::getStackNumber, code)
                .in(WmsInReceiptTask::getDocumentStatus, Arrays.asList("1", "2", "5")));
        if (CollectionUtils.isEmpty(tasks)) {
            throw new JeecgBootException("查询不到该托盘码");
        }
        if (tasks.size() > 1) {
            throw new JeecgBootException("有多条执行中的入库任务使用该托盘码");
        }
        if (StringUtils.isNotBlank(taskId) && !ObjectUtil.equal(taskId, tasks.get(0).getId())) {
            throw new JeecgBootException("该托盘码不属于该入库任务");
        }
        List<WmsInReceiptTaskMaterial> wmsInReceiptTaskMaterialList = wmsInReceiptTaskMaterialService.selectByMainId(tasks.get(0).getId());
        List<ListByMaterialIdResponseVO> list = org.jeecg.common.util.BeanUtil.copyToList(wmsInReceiptTaskMaterialList, ListByMaterialIdResponseVO.class);
        //过滤掉全部入库的
        list = list.stream().filter(s->!Objects.equals(s.getWarehouseStatus(), InboundStatusEnum.ALL.getCode())).collect(Collectors.toList());
        List<String> materialIds = list.stream().map(ListByMaterialIdResponseVO::getWmsBasicMaterialId).collect(Collectors.toList());
        List<BasicMaterial> basicMaterials = materialService.list(new LambdaQueryWrapper<BasicMaterial>()
                .in(CollectionUtil.isNotEmpty(materialIds), BasicMaterial::getId, materialIds));
        for (ListByMaterialIdResponseVO vo : list) {
            basicMaterials.forEach(basicMaterial -> {
                if (basicMaterial.getId().equals(vo.getWmsBasicMaterialId())) {
                    if (ObjectUtil.isNotEmpty(basicMaterial)) {
                        vo.setMaterialCode(basicMaterial.getCode());
                        vo.setMaterialName(basicMaterial.getName());
                        vo.setSpecification(basicMaterial.getSpecification());
                    }
                }
            });
        }
        return list;
    }

    /**
     * 出库扫托盘码
     * @param taskId
     * @param code
     * @return
     */
    @Override
    public List<ListByMaterialIdOutVO> outTrayCode(String taskId, String code) {

        List<String> collect = this.getBatchCodeList(code);
        List<WmsOutReceiptTaskMaterial> materials = wmsOutReceiptTaskMaterialService.list(new LambdaQueryWrapper<WmsOutReceiptTaskMaterial>()
                .eq(WmsOutReceiptTaskMaterial::getWmsOutReceiptTaskId, taskId)
                .in(WmsOutReceiptTaskMaterial::getBatchCode, collect));
        List<ListByMaterialIdOutVO> list = org.jeecg.common.util.BeanUtil.copyToList(materials, ListByMaterialIdOutVO.class);
        List<String> materialIds = list.stream().map(ListByMaterialIdOutVO::getWmsBasicMaterialId).collect(Collectors.toList());
        List<BasicMaterial> basicMaterials = materialService.list(new LambdaQueryWrapper<BasicMaterial>()
                .in(CollectionUtil.isNotEmpty(materialIds), BasicMaterial::getId, materialIds));
        for (ListByMaterialIdOutVO vo : list) {
            basicMaterials.forEach(basicMaterial -> {
                if (basicMaterial.getId().equals(vo.getWmsBasicMaterialId())) {
                    if (ObjectUtil.isNotEmpty(basicMaterial)) {
                        vo.setMaterialCode(basicMaterial.getCode());
                        vo.setMaterialName(basicMaterial.getName());
                        vo.setSpecification(basicMaterial.getSpecification());
                    }
                }
            });
        }
        return list;
    }

    /**
     * 移库扫托盘码
     * @param code
     * @return
     */
    @Override
    public List<WmsRepertory> moveTrayCode(String code) {
        if (StringUtils.isBlank(code)) {
            throw new JeecgBootException("托盘码不能为空");
        }
        List<WmsRepertory> repertories = wmsRepertoryService.list(new LambdaQueryWrapper<WmsRepertory>()
                .eq(WmsRepertory::getStackNumber, code));
        if (CollectionUtils.isEmpty(repertories)) {
            throw new JeecgBootException("查询不到该托盘码");
        }
        for (WmsRepertory repertory : repertories) {
            repertory.setRepertoryId(repertory.getId());
            repertory.setId(null);
        }
        return repertories;
    }

    /**
     * 拣货扫托盘码
     * @param taskId
     * @param code
     * @return
     */
    @Override
    public List<OutboundTaskUnPickVO> pickTrayCode(String taskId, String code) {
        List<String> batchCodeList = this.getBatchCodeList(code);
        List<OutboundTaskUnPickVO> vos = new ArrayList<>();
        for (String batchCode : batchCodeList) {
            List<OutboundTaskUnPickVO> list = wmsOutReceiptTaskPickService.getUnPickMaterialByNameAndTaskId(batchCode, taskId);
            vos.addAll(list);
        }
        return vos;
    }

    /**
     * 装车复核扫托盘码
     * @param taskId
     * @param code
     * @return
     */
    @Override
    public List<OutboundReceiptLoadCheckFullVO> checkTrayCode(String taskId, String code, String carNumber) {
        List<String> batchCodeList = this.getBatchCodeList(code);
        List<OutboundReceiptLoadCheckFullVO> vos = new ArrayList<>();
        for (String batchCode : batchCodeList) {
            List<OutboundReceiptLoadCheckFullVO> list = wmsOutReceiptLoadCheckService.getLoadCheckByNameAndTaskId(batchCode, taskId);
            if (StringUtils.isNotBlank(carNumber)) {
                list = list.stream().filter(a -> ObjectUtil.equal(carNumber, a.getCarNumber())).collect(Collectors.toList());
            }
            vos.addAll(list);
        }
        return vos;
    }

    /**
     * 根据托盘码在即时库存查询批次号
     * @param code
     * @return
     */
    private List<String> getBatchCodeList(String code) {
        if (StringUtils.isBlank(code)) {
            throw new JeecgBootException("托盘码不能为空");
        }
        List<WmsRepertory> repertories = wmsRepertoryService.list(new LambdaQueryWrapper<WmsRepertory>()
                .eq(WmsRepertory::getStackNumber, code));
        if (CollectionUtils.isEmpty(repertories)) {
            throw new JeecgBootException("查询不到该托盘码");
        }
        if (repertories.stream().anyMatch(a -> StringUtils.isBlank(a.getBatchCode()))) {
            throw new JeecgBootException("该托盘码关联的即时库存中数据批次号为空");
        }
        List<String> collect = repertories.stream().map(WmsRepertory::getBatchCode).collect(Collectors.toList());
        return collect;
    }

}
